package com.ideal.frame.sys.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ideal.frame.entity.TreeModel;
import com.ideal.frame.sys.entity.SysMenu;
import com.ideal.frame.sys.entity.SysRole;
import com.ideal.frame.sys.entity.SysUser;
import com.ideal.frame.util.DtoUtils;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * @Author: jin
 * @CreateTime: 2019-11-05 22:06
 * @Description: 用户转换类
 */
@Data
public class UserDto implements UserDetails {
    private Long id;
    //用户名
    private String username;
    //姓名
    private String realName;
    //密码
    @JsonIgnore
    private String password;
    //角色
    @JsonIgnore
    private List<SysRole> roles;
    private List<String> permissions;
    private List<TreeModel<SysMenu>> menus;

    public UserDto(SysUser sysUser){
        this.id = sysUser.getId();
        this.username = sysUser.getUsername();
        this.realName = sysUser.getRealName();
        this.password = sysUser.getPassword();
    }

    public UserDto(Long id,String username){
        this.id = id;
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(CollectionUtils.isEmpty(this.roles)){
            return null;
        }
        List<GrantedAuthority> authorities = DtoUtils.toListDto(this.roles,(SysRole r)->{
            return new SimpleGrantedAuthority(r.getName());
        });
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    /**
     * 账户是否未过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     *  账户是否未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    /**
     * 密码是否未过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /** 账户是否激活
     */
    @Override
    public boolean isEnabled() {
        return true;
    }


}
